package graphEd.saisie;



import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import table.EditorTable;
import DataStorage.EchantillonDAOHibernate;
import DataStorage.TNC_ObjectifDAOHibernate;
import HibernateSQLite.java.model.Echantillon;
import HibernateSQLite.java.model.TNC_Objectif;

import com.mxgraph.util.mxResources;

public class EditorEchantillon extends JPanel {
	private static final long serialVersionUID = 1L;
	private EditorTable table = new EditorTable(null, 5);
	private List<Long> oldEchantillon_id = new ArrayList<Long>();
	
	
	public EditorEchantillon(){
		this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		table.addRow(new String[]{mxResources.get("system"), mxResources.get("name"), mxResources.get("count"), mxResources.get("path"), mxResources.get("op")});
		table.addRow(new String[]{mxResources.get("mainPiece")});
		table.addRow(new String[]{mxResources.get("complementarySubgroup")});
		table.addRow(new String[]{mxResources.get("additionalSubgroup")});
		
		table.getTable().setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.setShowButtons(true);
		JScrollPane scrollPane = new JScrollPane(table.getTable());
		this.add(scrollPane);
	}
	
	public void create(long infoLigneId){
		EchantillonDAOHibernate echantillonDAO = new EchantillonDAOHibernate();
		//List<Echantillon> listEchantillon = new ArrayList<Echantillon>();// = echantillonDAO.getListEchantillon(infoLigneId);
		List<String> listOrgane = table.getValuesInColumn(0, 1);
		List<String> listNom = table.getValuesInColumn(1, 1);
		List<String> listNombre = table.getValuesInColumn(2, 1);
		List<String> listChemin = table.getValuesInColumn(3, 1);
		List<String> listOP = table.getValuesInColumn(4, 1);

		for(Long l:oldEchantillon_id){
			System.out.println("Suppression:"+l.intValue());
			echantillonDAO.deleteById(l);
		}
		
		echantillonDAO.saveEchantillon(listOrgane, listNom, listNombre, listChemin, listOP, infoLigneId);
		if (oldEchantillon_id.isEmpty()){
			for(Echantillon e:echantillonDAO.getListEchantillon(infoLigneId)){
				oldEchantillon_id.add(e.getEchantillon_id());
			}		
		}
		/*TNC_ObjectifDAOHibernate tncDAO = new TNC_ObjectifDAOHibernate();
		
		for(Long l:oldTnc_id){
			TNC_Objectif tnc = (TNC_Objectif)tncDAO.getSession().load(TNC_Objectif.class, l);
			tncDAO.saveOrUpdate(tnc);
		}*/
	}

	public void open(long infoLigneId){
		EchantillonDAOHibernate echantillonDAO = new EchantillonDAOHibernate();
		List<Echantillon> listEchantillon = echantillonDAO.getListEchantillon(infoLigneId);

		if(!listEchantillon.isEmpty()){
			for(Echantillon  echantillon :listEchantillon){
				oldEchantillon_id.add(echantillon.getEchantillon_id());
			}
					
			System.out.println("=>"+listEchantillon.size());
			for(int i=1; i<=3; i++){
				table.setValueAt(listEchantillon.get(i-1).getNom(), i, 1);
				table.setValueAt(listEchantillon.get(i-1).getNombre(), i, 2);
				table.setValueAt(listEchantillon.get(i-1).getChemin(), i, 3);
				table.setValueAt(listEchantillon.get(i-1).getChemin(), i, 4);
			}
		}
	}
}
